/* 
 * Copyright (C) 2012 UTSChedule
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package utsc.apps.utschedule.classes;

import android.content.Intent;

import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;

/**
 * The data model to represent an Exam for a course.
 * 
 * @author 
 * 
 */
@DatabaseTable
public class Exam extends GenericTask {

	public static final int TYPE_MIDTERM = 0;
	public static final int TYPE_FINAL = 3;
	// useful for db operation
	public static final String COURSE = "_course_id";
	public static final String EXAMID = "_examId";
	public static final String TIMETABLE = "_timetable_id";
	public static final String TYPE = "_type";
	public static String STARTTIME="_startTime";
	public static String ENDTIME="_endTime";
	public static String LOCATION="_location";
	public static String EXPIRED="_expired";
	
	public static final String[] TYPES={"Midterm","Final"};

	@DatabaseField(uniqueCombo=true, foreign = true, canBeNull = false)
	private Course _course;
	@DatabaseField(generatedId=true)
	private int _examId;
	@DatabaseField(foreign = true, canBeNull = false)
	private Timetable _timetable;
	@DatabaseField (uniqueCombo=true)
	private int _type = TYPE_MIDTERM;
	@DatabaseField
	private long _startTime;
	@DatabaseField
	private long _endTime;
	@DatabaseField
	private String _location;
	private boolean _expired=false;
	private String _dateString;
	private String _startTimeString;
	private String _endTimeString;
	
	public Exam() {
	}
	
	public String getTypeString(){
		if (_type==TYPE_MIDTERM) {
			return TYPES[0];
		}else {
			return TYPES[1];
		}
	}

	/**
	 * A useful constructor to take in an application intent and setup the object from it.
	 * @param intent
	 */
	public Exam(Intent intent){
		_examId=intent.getIntExtra(EXAMID, -1);
		_location=intent.getStringExtra(LOCATION);
		set_weight(intent.getDoubleExtra(WEIGHT, 0));
		set_mark(intent.getDoubleExtra(MARK, 0));
		set_total(intent.getDoubleExtra(TOTAL, 0));
		_startTime=intent.getLongExtra(STARTTIME,0);
		_endTime=intent.getLongExtra(ENDTIME, 0);
		_type=intent.getIntExtra(TYPE, TYPE_MIDTERM);
		_course=new Course();
		_course.set_courseID(intent.getIntExtra(COURSE, -1));
		_timetable=new Timetable();
		_timetable.set_timetableID(intent.getIntExtra(TIMETABLE, -1));
		_expired=intent.getBooleanExtra(EXPIRED, false);
	}
	
	/**
	 * A helper method to convert the current object into an application intent for data transfer among activities.
	 * @return the intent representation of the object
	 */
	public Intent getObjectIntent(){
		Intent intent=new Intent();
		intent.putExtra(EXAMID, _examId);
		intent.putExtra(COURSE, _course.get_courseID());
		intent.putExtra(TIMETABLE, _timetable.get_timetableID());
		intent.putExtra(LOCATION, _location);
		intent.putExtra(TYPE, _type);
		intent.putExtra(STARTTIME, _startTime);
		intent.putExtra(ENDTIME, _endTime);
		intent.putExtra(WEIGHT, get_weight());
		intent.putExtra(MARK, get_mark());
		intent.putExtra(TOTAL, get_total());
		intent.putExtra(EXPIRED, _expired);
		return intent;
	}
	
	public Course get_course() {
		return _course;
	}

	public void set_course(Course _course) {
		this._course = _course;
		set_timetable(_course.get_timetable());
	}

	public int get_examId() {
		return _examId;
	}

	public void set_examId(int _examId) {
		this._examId = _examId;
	}

	public Timetable get_timetable() {
		return _timetable;
	}

	public void set_timetable(Timetable _timetable) {
		this._timetable = _timetable;
	}

	public int get_type() {
		return _type;
	}

	public void set_type(int _type) {
		this._type = _type;
	}

	@Override
	public String toString() {
		if (_course!=null && _course.toString()!=null) {
			return _course.toString()+" - "+getTypeString();
		}
		return getTypeString();
	}

	public boolean is_expired() {
		return _expired;
	}

	public void set_expired(boolean _expired) {
		this._expired = _expired;
	}
	
	public long get_endTime() {
		return _endTime;
	}

	public long get_startTime() {
		return _startTime;
	}

	public void set_endTime(long _endTime) {
		this._endTime = _endTime;
	}

	public void set_startTime(long _startTime) {
		this._startTime = _startTime;
	}

	public String get_location() {
		return _location;
	}

	public void set_location(String _location) {
		this._location = _location;
	}

	public String get_dateString() {
		return _dateString;
	}

	public void set_dateString(String _dateString) {
		this._dateString = _dateString;
	}

	public String get_startTimeString() {
		return _startTimeString;
	}

	public void set_startTimeString(String _startTimeString) {
		this._startTimeString = _startTimeString;
	}

	public String get_endTimeString() {
		return _endTimeString;
	}

	public void set_endTimeString(String _endTimeString) {
		this._endTimeString = _endTimeString;
	}

	/**
	 * Copy the current {@link Exam exam} object to a new reference.
	 */
	@Override
	public Exam copyTask() {
		Exam newCopy=new Exam(getObjectIntent());
		return newCopy;
	}
	
}
